package com.edais.util.zsht;

import java.net.URLEncoder;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edais.util.CommonUtil;
import com.edais.util.JsonUtil;
import com.edais.util.ThirdPartyEnvUtil;
import com.edais.util.RSAThirdParty.RSA;

public class ZSHTPayThridPartyFunctionUtil {
	private static final Log logger = LogFactory.getLog(ZSHTPayThridPartyFunctionUtil.class);
	
	/**
	 * 支付申请
	 * @param paramMap
	 * @return
	 */
	public static Map<String,String> zshtTopUp(Map<String, String> paramMap){
		String url = ThirdPartyEnvUtil.getValue("zshtTopUp");

		String requestParam = "keyRandom=KEY_RANDOM";
		String publicKeyParam="";
		
		try {
			publicKeyParam = RSA.getPublick_Key_Encrypt(paramMap);
			requestParam = requestParam.replace("KEY_RANDOM", URLEncoder.encode(publicKeyParam, "utf-8"));
		} catch (Exception e) {
			logger.debug("公钥加密串："+e);
		}
		logger.debug("支付申请信息请求参数："+requestParam);
		
		String zshtTopUp =  CommonUtil.sendPostByHttp(url, requestParam);
		
		if( null == zshtTopUp ){
			logger.error("支付申请信息失败");
			return null;
		}else{
			//解析发送结果
			Map<String,String> resMap = JsonUtil.getMapFromJsonString(zshtTopUp);
			return resMap;
		}
	}
	
	/**
	 * 验证支付
	 * @param paramMap
	 * @return
	 */
	public static Map<String,String> checkTopUp(Map<String, String> paramMap){
		String url = ThirdPartyEnvUtil.getValue("zshtCheckTop");

		String requestParam = "keyRandom=KEY_RANDOM";
		String publicKeyParam="";
		
		try {
			publicKeyParam = RSA.getPublick_Key_Encrypt(paramMap);
			requestParam = requestParam.replace("KEY_RANDOM", URLEncoder.encode(publicKeyParam, "utf-8"));
		} catch (Exception e) {
			logger.debug("公钥加密串："+e);
		}
		logger.debug("验证支付信息请求参数："+requestParam);
		
		String zshtCheckTop =  CommonUtil.sendPostByHttp(url, requestParam);
		
		if( null == zshtCheckTop ){
			logger.error("验证支付信息失败");
			return null;
		}else{
			//解析发送结果
			Map<String,String> resMap = JsonUtil.getMapFromJsonString(zshtCheckTop);
			return resMap;
		}
	}
	
	/**
	 * 银行卡验证
	 * @param paramMap
	 * @return
	 */
	public static Map<String,String> bankCardAndPhoneCheck(Map<String, String> paramMap){
		String url = ThirdPartyEnvUtil.getValue("bankCardAndPhoneCheck");
		logger.info("##########进入掌上汇通银行卡验证方法###########");
		logger.info("url:"+url);
		String requestParam = "keyRandom=KEY_RANDOM";
		String publicKeyParam="";
		try {
			publicKeyParam = RSA.getPublick_Key_Encrypt(paramMap);
			requestParam = requestParam.replace("KEY_RANDOM", URLEncoder.encode(publicKeyParam, "utf-8"));
		} catch (Exception e) {
			logger.debug("公钥加密串："+e);
		}
		logger.debug("银行卡验证信息请求参数："+requestParam);
		
		String bankCardAndPhoneCheck =  CommonUtil.sendPostByHttp(url, requestParam);
		
		if( null == bankCardAndPhoneCheck ){
			logger.error("银行卡验证信息失败");
			return null;
		}else{
			//解析发送结果
			Map<String,String> resMap = JsonUtil.getMapFromJsonString(bankCardAndPhoneCheck);
			return resMap;
		}
	}
	
	/**
	 * 调用第三方发送短信
	 * @param paramMap
	 * @return
	 */
	public static Map<String,String> sendErrorMessage(Map<String, String> paramMap){
		String url =  ThirdPartyEnvUtil.getValue("smsUrl");
		String requestParam = "keyRandom=KEY_RANDOM";
		String publicKeyParam="";
		
		try {
			publicKeyParam = RSA.getPublick_Key_Encrypt(paramMap);
			requestParam = requestParam.replace("KEY_RANDOM", URLEncoder.encode(publicKeyParam, "utf-8"));
		} catch (Exception e) {
			logger.debug("公钥加密串："+e);
		}
		logger.debug("调用第三方发送短信信息请求参数："+requestParam);
		
		String sendErrorMessage =  CommonUtil.sendPostByHttp(url, requestParam);
		
		
		if( null == sendErrorMessage ){
			logger.error("调用第三方发送短信信息失败");
			return null;
		}else{
			//解析发送结果
			Map<String,String> sendErrorMessageMap = JsonUtil.getMapFromJsonString(sendErrorMessage);
			return sendErrorMessageMap;
		}
	}
}
